package com.zizo.autohome.sta.competition.updateOld;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zizo.util.ConnectionPool;

public class CompDao implements Runnable {
	private Connection conn;
	private Comp comp;

	public CompDao(Connection conn, Comp comp) {
		super();
		this.conn = conn;
		this.comp = comp;
	}

	public List<Comp> getData() {
		List<Comp> list = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT competion_id,competion_core,competion_rel,competion_link_old FROM carvoice_biz.biz_competion_join WHERE competion_id NOT IN (SELECT brand_id FROM carvoice_biz.biz_brand WHERE brand_autohome NOT IN (SELECT p.price_cid FROM autohome_other.other_price AS p WHERE price_isTrue=1)) AND competion_rel  NOT IN (SELECT brand_id FROM carvoice_biz.biz_brand WHERE brand_autohome NOT IN (SELECT p.price_cid FROM autohome_other.other_price AS p WHERE price_isTrue=1));";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int cid = rs.getInt(1);
				int core = rs.getInt(2);
				int rel = rs.getInt(3);
				double olink = rs.getDouble(4);
				Comp comp = new Comp(cid, core, rel, olink);
				list.add(comp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void update() {
		PreparedStatement ps = null;
		int core = comp.getCore();
		int rel = comp.getRel();
		int[] c = getTemp(core);
		int[] r = getTemp(rel);
		double cl = getLcj(core);
		double rl = getLcj(rel);
		double x1 = 0, x2 = 0, x3 = 0, x4 = 0;
		if (c[0] == r[0]) {
			x1 = 0.3;
		} else {
			x1 = 1;
		}
		int diff1 = Math.abs(c[1] - r[1]);
		if (diff1 == 0) {
			x2 = 1;
		} else if (diff1 == 1) {
			x2 = 0.8;
		} else if (diff1 == 2) {
			x2 = 0.5;
		} else if (diff1 == 3) {
			x2 = 0.3;
		}
		if (c[2] == r[2]) {
			x3 = 0.8;
		} else {
			x3 = 1;
		}
		double temp = cl > rl ? rl : cl;
		if (temp == 0) {
			return;
		}
		double diff2 = Math.abs(cl - rl) / temp;
		if (diff2 >= 0 && diff2 < 1) {
			x4 = 1;
		} else if (diff2 >= 0.1 && diff2 < 0.3) {
			x4 = 0.9;
		} else if (diff2 >= 0.3 && diff2 < 0.5) {
			x4 = 0.7;
		} else if (diff2 >= 0.5 && diff2 < 0.7) {
			x4 = 0.5;
		} else if (diff2 >= 0.7 && diff2 < 0.9) {
			x4 = 0.3;
		} else if (diff2 >= 0.9 && diff2 <= 1) {
			x4 = 0.1;
		}
		double res = Math.sqrt(Math.sqrt(comp.getOlink()) * 10) * 10 * x1 * x2 * x3 * x4;
		String sql = "UPDATE carvoice_biz.biz_competion_join SET competion_link=" + res + " WHERE competion_id="
				+ comp.getCid();
		try {
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionPool.close(ps);
		}
	}

	public int[] getTemp(int cid) {
		int[] res = new int[3];
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT brand_umbrella,brand_class,brand_body FROM carvoice_biz.biz_brand WHERE brand_id=" + cid;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			if (rs.next()) {
				int umb = rs.getInt(1);
				int cla = rs.getInt(2);
				int body = rs.getInt(3);
				res[0] = umb;
				res[1] = cla;
				res[2] = body;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

	public double getLcj(int cid) {
		double res = 0;
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT AVG(price_lcj) FROM autohome_other.other_price WHERE price_istrue=1 AND price_cid=(SELECT brand_autohome FROM carvoice_biz.biz_brand WHERE brand_id="
				+ cid + ") GROUP BY price_cid;";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			if (rs.next()) {
				res = rs.getDouble(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

	public void run() {
		update();
		System.out.println(comp.getCid());
	}

	public static void main(String[] args) {
		Connection conn = ConnectionPool.getConnection();
		CompDao dao = new CompDao(conn, null);
		List<Comp> list = dao.getData();
		ExecutorService fix = Executors.newFixedThreadPool(25);
		for (Comp c : list) {
			fix.execute(new CompDao(conn, c));
		}
		fix.shutdown();
	}
}
